#ifndef _IAP2_H_
#define _IAP2_H_

#include "compiler.h"

//for temporary
//#define mop_api_iic_read_data(a, b, c)
//#define mop_api_iic_write_data(a, b, c)

#define MFI_IC_PAGE_SIZE  128

#define AUTH_INIT_DATA_LEN               6
#define SYNC_PACKET_LEN                  23
#define IAP2_ACK_LEN                     9
#define AUTHEN_CERTIFICATION_PACKET_LEN  16
#define CHALLENGE_RESPONSE_LEN           40
#define START_IDENTIFICATION_LEN         16
#define START_CHARGE_SET_LEN             27
//#define START_POWER_UPDATES_LEN          28
#define START_POWER_UPDATES_LEN          24
#define STOP_POWER_UPDATES_LEN           16

//IAP2 Packet parameter define
#define START_PACKET_MSB   0xFF
#define START_PACKET_LSB   0x5A
#define IAP2_LINK_VERSION  0x01
#define MAX_NUM_OUTSTANDING_PACKET  0x01
#define MAX_PACKET_LEN      (32768+30)
#define RETRANSMISSION_TIMEOUT 1500
#define CUMULATIVE_ACK_TIMEOUT 0
#define MAX_NUM_RETRANSMISSION 0x0a
#define MAX_CUMULATIVE_ACK     0x00
#define CONTROL_SESSION_ID     0x0a
#define CONTROL_SESSION_TYPE   0x00
#define EA_SESSION_ID           0x0b
#define EA_SESSION_TYPE         0x02
#define SESSION_VERSION         0x01
#define SYCN_PACKET_LEN         26
#define CONTROL_SESSION_TYPE    0x00
#define SESSION_VERSION         0x01
#define AUTHEN_CERTIFICATION_PACKET_LEN 16
#define CHALLENGE_RESPONSE_LEN           40
#define START_IDENTIFICATION_LEN         16
#define AUTHEN_SUCCESS_LEN                16
#define IDENTIFICATION_ACCETP_LEN        16
#define EA_SESSION_START_LEN              27
#define IAP2_ACK_LEN                       9
#define SEND_APPLE_LUNCH_LEN             47

#define CHARGE_MODE_FULL             3
#define CHARGE_MODE_NORMAL           2
#define CHARGE_MODE_HALF             1   
#define CHARGE_MODE_OFF              0

#define CHARGE_VOLTAGE_1000               1000
#define CHARGE_VOLTAGE_800                1000
#define CHARGE_VOLTAGE_500                500

enum iap2_status_enum
{
    IAP2_IDLE = 0,
    IAP2_AUTHEN_INITIALIZATION = 1,
    IAP2_SYNC_PRE,
    IAP2_SYNC_SEND,
    IAP2_SYNC_COMPLETE,
    IAP2_SYNC_OVER,
    IAP2_REQ_AUTHEN_CERTIFICATION,
    IAP2_SEND_AUTHEN_CERTIFICATION,
    IAP2_REC_AUTHEN_CERTIFICATION_RESPONSE,
    IAP2_REC_CHALLENGE_RESPONSE,
    IAP2_SEND_CHALLENGE,
    IAP2_ACK_CHALLENGE,
    IAP2_REC_AUTHEN_SUCCESS,
    IAP2_ACK_AUTHEN_SUCCESS,
    IAP2_REC_START_IDENTIFICATION,
    IAP2_SEND_IDENTIFICATION,
    IAP2_ACK_IDENTIFICATION,
    IAP2_REC_IDENTIFICATION_ACCEPT,
    IAP2_ACK_IDENTIFICATION_ACCEPT,
    IAP2_SEND_START_POWER_UPDATE,
    IAP2_REC_START_POWER_UPDATE_ACK,
    IAP2_SEND_POWER_UPDATE,
    IAP2_REC_POWER_UPDATE_ACK,
    IAP2_REC_POWER_UPDATE_DATA,
    IAP2_ACK_POWER_UPDATE_DATA,
    IAP2_REC_POWER_UPDATE_DATA_AGAIN,
    IAP2_SEND_STOP_POWER_UPDATE,
    IAP2_REC_STOP_POWER_UPDATE_ACK,
    IAP2_SEND_APPLE_LUNCH,
    IAP2_REC_APPLE_LUNCH_ACK,
};

extern uint8_t g_iap2_status;
extern uint8_t iap2_packet_sequence_num;
extern uint8_t iap2_packet_ack_num;
extern uint16_t certification_tx_len;

bool iap2_start_sequence(void);
bool iap2_set_charge_config(uint8_t charge_config);

//#define start_iap2_sequence(a, b) udd_ep_send_one_pkt((IAP2_EP_IN & 0x0f), a, b)
#define receive_iap2_pkt(a, b) udd_ep_receive_one_pkt(IAP2_EP_OUT & 0x0f, a, b)
//#define start_iap2_sequence_iOS6(a, b) udd_ep_send_one_pkt((IAP2_EP_IN & 0x0f), a, b); udd_ep_send_one_pkt((INTR_EP_IN & 0x0f), NULL, 0)

#endif
